package cn.stylefeng.guns.modular.addressbook.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.addressbook.entity.Department;
import cn.stylefeng.guns.modular.addressbook.enums.DepartmentExceptionEnum;
import cn.stylefeng.guns.modular.addressbook.mapper.DepartmentMapper;
import cn.stylefeng.guns.modular.addressbook.pojo.request.DepartmentRequest;
import cn.stylefeng.guns.modular.addressbook.service.DepartmentService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Date;

/**
 * 业务实现层
 *
 * @author xbz
 * @date 2023/04/27 09:19
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

	@Override
    public void add(DepartmentRequest departmentRequest) {
        Department department = new Department();
        BeanUtil.copyProperties(departmentRequest, department);
        this.save(department);
    }

    @Override
    public void del(DepartmentRequest departmentRequest) {
        Department department = this.queryDepartment(departmentRequest);
        this.removeById(department.getId());
    }

    @Override
    public void edit(DepartmentRequest departmentRequest) {
        Department department = this.queryDepartment(departmentRequest);
        BeanUtil.copyProperties(departmentRequest, department);
        this.updateById(department);
    }

    @Override
    public Department detail(DepartmentRequest departmentRequest) {
        return this.queryDepartment(departmentRequest);
    }

    @Override
    public PageResult<Department> findPage(DepartmentRequest departmentRequest) {
        LambdaQueryWrapper<Department> wrapper = createWrapper(departmentRequest);
        Page<Department> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<Department> findList(DepartmentRequest departmentRequest) {
        LambdaQueryWrapper<Department> wrapper = this.createWrapper(departmentRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author xbz
     * @date 2023/04/27 09:19
     */
    private Department queryDepartment(DepartmentRequest departmentRequest) {
        Department department = this.getById(departmentRequest.getId());
        if (ObjectUtil.isEmpty(department)) {
            throw new ServiceException(DepartmentExceptionEnum.DEPARTMENT_NOT_EXISTED);
        }
        return department;
    }

    /**
     * 创建查询wrapper
     *
     * @author xbz
     * @date 2023/04/27 09:19
     */
    private LambdaQueryWrapper<Department> createWrapper(DepartmentRequest departmentRequest) {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();

        Long id = departmentRequest.getId();
        String departmentName = departmentRequest.getDepartmentName();
        String delFlag = departmentRequest.getDelFlag();

        queryWrapper.eq(ObjectUtil.isNotNull(id), Department::getId, id);
        queryWrapper.like(ObjectUtil.isNotEmpty(departmentName), Department::getDepartmentName, departmentName);
        queryWrapper.like(ObjectUtil.isNotEmpty(delFlag), Department::getDelFlag, delFlag);

        return queryWrapper;
    }

}